package com.ycsj.goldmedalnewconcept.master.activity;

import android.annotation.SuppressLint;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.mob.MobSDK;
import com.ycsj.common.base.BaseActivity;
import com.ycsj.common.base.BaseApplication;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.common.HttpAddress;
import com.ycsj.goldmedalnewconcept.common.http.HttpCallBack;
import com.ycsj.goldmedalnewconcept.common.http.HttpUtils;
import com.ycsj.goldmedalnewconcept.common.util.AccountUtils;
import com.ycsj.goldmedalnewconcept.master.adapter.GetRedLvAdapter;
import com.ycsj.goldmedalnewconcept.master.bean.GetRedInfo;
import com.ycsj.goldmedalnewconcept.view.PullToRefreshLayout;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.Platform.ShareParams;
import cn.sharesdk.onekeyshare.OnekeyShare;
import cn.sharesdk.onekeyshare.ShareContentCustomizeCallback;
import cn.sharesdk.sina.weibo.SinaWeibo;

/**
 * 红包领取记录
 */

@SuppressLint("NewApi")
public class MasterRedPackageGetRecordActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener, View.OnClickListener {

    //正常请求
    public static final int NORMAL = 0X110;

    //下拉刷新
    public static final int PULLTOREFRESH = 0X120;


    //返回
    private ImageView iv_back;

    //加载视图
    private LinearLayout llLoading;

    //空数据
    private LinearLayout llEmpty;

    //下拉刷新控件
    private PullToRefreshLayout ptrl;

    //列表控件
    private ListView lv;

    //头布局
    private View headView;
    //头布局的布局控件
    private TextView tvSend;//总发出
    private TextView tvNum;//总领取人数
    private TextView tvUse;//总使用
    private ImageView lv_edit;//分享
    private TextView tv_title;//标题


    //列表控件的数据集合
    private List<GetRedInfo.RedPacketsBean.PersonsBean> oldDatas;
    private String redid;
    private GetRedLvAdapter adapter;

    private String title;

    private String Money;

    //加载框
    private RelativeLayout rlLoading;

    @Override
    protected int getContentLayout() {
        return R.layout.activity_red_package_get_record;
    }

    @Override
    protected void init() {


        //状态栏
        initStateBar();

        //获取红包ID
        getRedId();

        //初始化View的方法
        initViews();

        initTitle();

        //初始化列表
        initLv();

        //设置监听
        initListener();

        //网络请求数据
        http(NORMAL);
    }


    /**
     * 设置标题
     */
    private void initTitle() {

        tv_title.setText("红包明细");
        lv_edit.setVisibility(View.VISIBLE);
        lv_edit.setImageResource(R.drawable.base_share);
    }


    /**
     * 网络请求的方法
     *
     * @param method
     */
    private void http(final int method) {
        Map<String, String> map = new HashMap<>();
        map.put("type", "7");
        map.put("redId", redid);
        map.put("role", AccountUtils.getRole(this));
//        map.put("apptoken", AccountUtils.getToken(this));
//        map.put("platform", "2");

        HttpUtils.post(HttpAddress.PARENTREDLIST, map, this, new HttpCallBack<GetRedInfo>() {
            @Override
            public void onSuccess(GetRedInfo info) {
                rlLoading.setVisibility(View.GONE);
                if ("1".equals(info.getState())) {

                    final GetRedInfo.RedPacketsBean redPackets = info.getRedPackets();
                    //获取列表数据
                    final List<GetRedInfo.RedPacketsBean.PersonsBean> skins = redPackets.getPersons();
                    if (null != skins && skins.size() != 0) {

                        Collections.reverse(skins);//倒序处理

                        //证明有列表数据,更新主界面
                        //设置头部数据
                        tvSend.setText(redPackets.getMoney() + "");
                        tvNum.setText(redPackets.getRobbedPerson() + " 人");
//                                    tvNum.setText(skins.size() + " 人");
                        tvUse.setText(redPackets.getUsedMoney() + "");

                        if (method == NORMAL) {
                            //正常请求
                            if (llLoading != null) {
                                llLoading.setVisibility(View.GONE);
                            }
                            oldDatas.addAll(skins);
                            lv.setAdapter(adapter);
                        } else {
                            //下拉刷新
                            oldDatas.clear();
                            oldDatas.addAll(skins);
                            ptrl.refreshFinish(PullToRefreshLayout.REFRESH_SUCCEED);
                            adapter.notifyDataSetChanged();

                        }
                    } else {
                        //证明没有数据,显示空视图,隐藏加载框
                        if (method == NORMAL) {
                            tvSend.setText(Money + "");
                            tvNum.setText("0 人");
                            tvUse.setText("0");
                            //正常加载
                            if (llLoading != null) {
                                llLoading.setVisibility(View.GONE);
                            }
                            if (llEmpty != null) {
                                llEmpty.setVisibility(View.VISIBLE);
                            }
                            lv.setAdapter(adapter);
                        } else {
                            //下拉刷新
                            ptrl.refreshFinish(PullToRefreshLayout.REFRESH_SUCCEED);
                        }
                    }
                } else {
                    ToastUtils.showShort(BaseApplication.STATEERROE + info.getState());
                }
            }

            @Override
            public void onError(String e) {
                rlLoading.setVisibility(View.GONE);
                ToastUtils.showShort(BaseApplication.ERROR);
            }
        });
    }

    /**
     * 初始化View的方法
     */
    private void initViews() {
        iv_back = findViewById(R.id.iv_back);
        llLoading = findViewById(R.id.ll_loading);
        ptrl = findViewById(R.id.refresh_view);
        lv_edit = findViewById(R.id.lv_edit);
        tv_title = findViewById(R.id.tv_title);

        lv = findViewById(R.id.lv);
        llEmpty = findViewById(R.id.ll_empty);
        headView = View.inflate(this, R.layout.lv_getred_headview, null);
        tvSend = headView.findViewById(R.id.tv_send);
        tvNum = headView.findViewById(R.id.tv_num);
        tvUse = headView.findViewById(R.id.tv_use);
        rlLoading = findViewById(R.id.rl_loading);

    }

    /**
     * 获取红包ID的方法
     */
    public void getRedId() {
        redid = getIntent().getStringExtra("redid");
        title = getIntent().getStringExtra("title");
        Money = getIntent().getStringExtra("Money");

    }

    /**
     * 初始化列表控件的方法
     */
    private void initLv() {
        oldDatas = new ArrayList<GetRedInfo.RedPacketsBean.PersonsBean>();
        adapter = new GetRedLvAdapter(this, oldDatas, R.layout.item_lv_getpack);
        //给ListView添加头布局
        lv.addHeaderView(headView);
    }

    /**
     * 设置监听的方法
     */
    private void initListener() {
        iv_back.setOnClickListener(this);
        lv_edit.setOnClickListener(this);
        ptrl.setOnRefreshListener(this);
    }


    /**
     * 下拉刷新
     */
    @Override
    public void onRefresh() {
        http(PULLTOREFRESH);
    }

    /**
     * 点击事件
     *
     * @param view
     */
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                //返回
                finish();
                break;
            case R.id.lv_edit:
                //分享
                share();

                break;
        }
    }

    private void share() {
        // TODO Auto-generated method stub
        MobSDK.init(this, "12c24ea0b59b0",
                "aae63565fbd69b1acbfcdd0d5ac0b35c");
        OnekeyShare oks = new OnekeyShare();
        // 关闭sso授权
        oks.disableSSOWhenAuthorize();
        oks.setShareContentCustomizeCallback(new ShareContentCustomizeCallback() {

            @Override
            public void onShare(Platform platform, ShareParams paramsToShare) {

                if (SinaWeibo.NAME.equals(platform.getName())) {
                    paramsToShare.setTitle(getString(R.string.share));

                    String title = "来自"
                            + AccountUtils.getSchoolName(MasterRedPackageGetRecordActivity.this)
                            + "学校的红包";
                    paramsToShare.setTitleUrl(HttpAddress.TESTREDPAG
                            + "?rid=" + redid);
                    paramsToShare.setTitle("来自"
                            + AccountUtils.getSchoolName(MasterRedPackageGetRecordActivity.this)
                            + "学校的红包");
                    paramsToShare.setText(title);

                    paramsToShare
                            .setImageUrl("http://139.129.57.83:8080/Appandroid/redbag.png");

                    // url仅在微信（包括好友和朋友圈）中使用
                    paramsToShare.setUrl(HttpAddress.TESTREDPAG
                            + "?rid=" + redid);
                    // paramsToShare.setUrl("http://m.moregolden.com:36356/redBag/redBag.html?rid="+redid);
                    // comment是我对这条分享的评论，仅在人人网和QQ空间使用
                    paramsToShare.setComment("");
                    // site是分享此内容的网站名称，仅在QQ空间使用
                    // paramsToShare.setSite(getString(R.string.app_name));
                    paramsToShare.setSite("金牌君");
                    // siteUrl是分享此内容的网站地址，仅在QQ空间使用
                    paramsToShare.setSiteUrl(HttpAddress.TESTREDPAG
                            + "?rid=" + redid);
                    // paramsToShare.setSiteUrl("http://m.moregolden.com:36356/redBag/redBag.html?rid="+redid);

                } else {
                    paramsToShare.setShareType(Platform.SHARE_WEBPAGE);
                    paramsToShare.setTitle(getString(R.string.share));
                    // titleUrl是标题的网络链接，仅在人人网和QQ空间使用
                    paramsToShare.setTitleUrl(HttpAddress.TESTREDPAG
                            + "?rid=" + redid);
                    // paramsToShare.setTitleUrl("http://m.moregolden.com:36356/redBag/redBag.html?rid="+redid);
                    paramsToShare.setTitle("来自"
                            + AccountUtils.getSchoolName(MasterRedPackageGetRecordActivity.this)
                            + "学校的红包");
                    // text是分享文本，所有平台都需要这个字段
                    paramsToShare.setText(title);
                    // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
                    // oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
                    paramsToShare
                            .setImageUrl("https://moregolden.com:8081/Appandroid/redbag.png");

                    // paramsToShare.setImageData(mBitmap);
                    // url仅在微信（包括好友和朋友圈）中使用
                    paramsToShare.setUrl(HttpAddress.TESTREDPAG
                            + "?rid=" + redid);
                    // paramsToShare.setUrl("http://m.moregolden.com:36356/redBag/redBag.html?rid="+redid);
                    // comment是我对这条分享的评论，仅在人人网和QQ空间使用
                    paramsToShare.setComment("");
                    // site是分享此内容的网站名称，仅在QQ空间使用
                    paramsToShare.setSite("金牌君");
                    // paramsToShare.setSite(getString(R.string.app_name));
                    // siteUrl是分享此内容的网站地址，仅在QQ空间使用
                    paramsToShare.setSiteUrl(HttpAddress.TESTREDPAG
                            + "?rid=" + redid);

                }
            }
        });

        // 启动分享GUI
        oks.show(this);
    }


}
